﻿Option Strict On
Option Explicit On
Imports FSUIPC
''' <summary>
''' G15FS9 - interface between Flight Simulator 2004/FS9 to the G15v2 Keyboard..
''' (c) 2009 Shayne Bouda.
''' FSUIPC / LgLCD.NET.DLL.
''' http://g15fs9.codeplex.com
''' </summary>
''' <remarks></remarks>
Module G15FS9_Main
    Public lcd As New lcd(AddressOf ButtonPress, AddressOf configPress)
    Public FontType As String = "Arial"
    Public FontSize As Integer = 9
    Public MainTimer As New Timer
    Public CheckTimer As New Timer
    Public Notify As New NotifyIcon
    Public NotifyMenu As New ContextMenu

#Region "Declarations"
    Dim Units As String
    Dim Screen_Page As Integer
#End Region

#Region "Program Wide FSUIPC Addresses"
    Dim Int_Units As Offset(Of Short) = New FSUIPC.Offset(Of Short)("config", &HC18)
#End Region

#Region "Main"
    Sub Main()
        ' Set default values if none are set yet in my.settings.
        If My.Settings.UpdateTimer = 0 And My.Settings.ConnectTimer = 0 Then
            My.Settings.UpdateTimer = 500
            My.Settings.ConnectTimer = 10000
            My.Settings.Save()
        End If

        ' Notify icon
        Notify.Icon = My.Resources.LeftThin
        Notify.Text = "G15-FS9"
        Notify.Visible = True
        Notify.ContextMenu = NotifyMenu
        NotifyMenu.MenuItems.Add("Exit", AddressOf ExitApplication)

        ' Get Config Settings for Screen.
        If My.Settings.Screen = 0 Then
            Screen_Page = 1
        Else
            Screen_Page = My.Settings.Screen
        End If

        ' Main Timer Settings.
        MainTimer.Interval = My.Settings.UpdateTimer
        AddHandler MainTimer.Tick, AddressOf Current_Screen

        ' Check Timer Settings.
        CheckTimer.Interval = My.Settings.ConnectTimer
        AddHandler CheckTimer.Tick, AddressOf CheckFS

        ' Init the LCD.
        lcd.InitLCD("G15-FS9")

        ' Start Check Timer.
        CheckTimer.Start()

        ' Run application consitently.
        Application.Run()
    End Sub
#End Region

#Region "Check FS is Running."
    Public Sub CheckFS(ByVal Sender As Object, ByVal e As System.EventArgs)
        Dim img As New System.Drawing.Bitmap(160, 43)
        Dim screen As Drawing.Graphics = Graphics.FromImage(img)
        FSUIPCConnection.Close()
        Try
            FSUIPCConnection.Open()
            CheckTimer.Stop()
            MainTimer.Start()
        Catch exFSUIPC As FSUIPCException
            If exFSUIPC.FSUIPCErrorCode = FSUIPCError.FSUIPC_ERR_NOFS Then
                screen.DrawString("G15-FS9", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(58, 1, 160, 43))
                screen.DrawString("FS / FSUIPC Not Running?", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(20, 15, 160, 43))
                lcd.DrawLCD(img)
                lcd.UpdateLCD()
            End If
        End Try
    End Sub
#End Region

#Region "Screen when an Error Occurrs."
    Public Sub WaitScreen()
        FSUIPCConnection.Close()
        MainTimer.Stop()
        CheckTimer.Start()
    End Sub
#End Region

#Region "Soft Buttons - Menu/Screen Control"
    Public Function ButtonPress(ByVal device As Integer, ByVal dwButtons As Integer, ByVal pContext As System.IntPtr) As Integer
        Select Case dwButtons
            Case 1
                ' Button 1 - Show Menu
                If Screen_Page = 10 Then Screen_Page = 1
                If Screen_Page = 11 Then Screen_Page = 2
                If Screen_Page = 12 Then Screen_Page = 3
                If Screen_Page = 13 Then Screen_Page = 4
                If Screen_Page = 14 Then Screen_Page = 5

            Case 2
                ' Button 2 - Select Menu.
                If Screen_Page = 1 Then Screen_Page = 10
                If Screen_Page = 2 Then Screen_Page = 11
                If Screen_Page = 3 Then Screen_Page = 12
                If Screen_Page = 4 Then Screen_Page = 13
                If Screen_Page = 5 Then Screen_Page = 14
            Case 4
                ' Button 3 - Scroll to prev menu item.
                If Screen_Page <= 5 Then
                    If Screen_Page = 1 Then Screen_Page = 1 Else Screen_Page -= 1
                End If
            Case 8
                ' Button 4 - Scroll to next menu item.
                If Screen_Page <= 5 Then
                    If Screen_Page = 5 Then Screen_Page = 5 Else Screen_Page += 1
                End If
        End Select
    End Function
#End Region

#Region "Screen Control"
    Public Sub Current_Screen(ByVal Sender As Object, ByVal e As System.EventArgs)
        Dim img As New System.Drawing.Bitmap(160, 43)
        Dim screen As Drawing.Graphics = Graphics.FromImage(img)
        Dim StrFormat As New Drawing.StringFormat
        StrFormat.Alignment = StringAlignment.Center
        StrFormat.LineAlignment = StringAlignment.Center

        Select Case Screen_Page
            Case 1 ' Show menu item 1.
                screen.DrawString("Altitude / Speed", New Font(FontType, 12, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(0, 0, 160, 43), StrFormat)
                screen.DrawString("Select", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(43, 32, 160, 43))
                ' Icons.
                screen.DrawIconUnstretched(My.Resources.LeftThin, New Rectangle(95, 39, 16, 16))
                screen.DrawIconUnstretched(My.Resources.RightThin, New Rectangle(138, 39, 16, 16))
                lcd.DrawLCD(img)
                lcd.UpdateLCD()
                '
            Case 2 ' Show menu item 2.
                screen.DrawString("Fuel Display A", New Font(FontType, 12, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(0, 0, 160, 43), StrFormat)
                screen.DrawString("Select", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(43, 32, 160, 43))
                ' Icons.
                screen.DrawIconUnstretched(My.Resources.LeftThin, New Rectangle(95, 39, 16, 16))
                screen.DrawIconUnstretched(My.Resources.RightThin, New Rectangle(138, 39, 16, 16))
                lcd.DrawLCD(img)
                lcd.UpdateLCD()
                '
            Case 3 ' Show menu item 3.
                screen.DrawString("Fuel Display B", New Font(FontType, 12, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(0, 0, 160, 43), StrFormat)
                screen.DrawString("Select", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(43, 32, 160, 43))
                ' Icons.
                screen.DrawIconUnstretched(My.Resources.LeftThin, New Rectangle(95, 39, 16, 16))
                screen.DrawIconUnstretched(My.Resources.RightThin, New Rectangle(138, 39, 16, 16))
                lcd.DrawLCD(img)
                lcd.UpdateLCD()
                ' 
            Case 4 ' Show menu item 4.
                screen.DrawString("Throttle Position", New Font(FontType, 12, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(0, 0, 160, 43), StrFormat)
                screen.DrawString("Select", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(43, 32, 160, 43))
                ' Icons.
                screen.DrawIconUnstretched(My.Resources.LeftThin, New Rectangle(95, 39, 16, 16))
                screen.DrawIconUnstretched(My.Resources.RightThin, New Rectangle(138, 39, 16, 16))
                lcd.DrawLCD(img)
                lcd.UpdateLCD()
            Case 5 ' Show menu item 5.
                screen.DrawString("Average Fuel Usage", New Font(FontType, 12, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(0, 0, 160, 43), StrFormat)
                screen.DrawString("Select", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(43, 32, 160, 43))
                ' Icons.
                screen.DrawIconUnstretched(My.Resources.LeftThin, New Rectangle(95, 39, 16, 16))
                screen.DrawIconUnstretched(My.Resources.RightThin, New Rectangle(138, 39, 16, 16))
                lcd.DrawLCD(img)
                lcd.UpdateLCD()

            Case 10 ' Screens according to Menu Selections - Starts at 10.
                Altitude_Knots.Display_Screen()
            Case 11
                Fuel.Display_Screen_A()
            Case 12
                Fuel.Display_Screen_B()
            Case 13
                Throttle_Pos.Display_Screen()
            Case 14
                Average_Fuel.Display_Screen()
        End Select

    End Sub
#End Region

#Region "Configure Screen"
    Public Function configPress(ByVal connection As Integer, ByVal pcontext As System.IntPtr) As Integer
        'Called when the "config" buton is pressed in Logitech LCD Manager.
        Configure.ShowDialog()
    End Function
#End Region

#Region "Clean and Close Application"
    Public Sub ExitApplication(ByVal sender As Object, ByVal e As EventArgs)
        ' Save Screen Settings.
        My.Settings.Screen = Screen_Page
        My.Settings.Save()
        ' Start Disposing.
        lcd.CloseLCD()
        MainTimer.Stop()
        CheckTimer.Stop()
        MainTimer.Dispose()
        CheckTimer.Dispose()
        Notify.Dispose()
        NotifyMenu.Dispose()
        ' Close FSUIPC Connection.
        FSUIPCConnection.Close()
        ' Exit Application.
        Application.Exit()
    End Sub
#End Region

End Module
